﻿using Dingding.Helper.Redis;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



namespace LTWebApi
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {











            string conn_redis = "127.0.0.1:6780,password=SH_softsight";











            using (var redisHelper = new StackExchange_RedisV1(conn_redis))
            {
                // 写入单条记录（同步）
                bool keySuccess = redisHelper.SetValue("key1", "value1", TimeSpan.FromSeconds(30));




                // 设置缓存
                var user = new User { Id = 1, Name = "John Doe soft sight", Email = "john@example.com" };



                var serializedValue = JsonConvert.SerializeObject(user);


                bool setSuccess = redisHelper.SetValue("user:1", serializedValue, TimeSpan.FromHours(1));

               // bool setSuccess = _cacheManager.Set("user:1", user, TimeSpan.FromHours(1));


                Console.WriteLine($"设置缓存是否成功: {setSuccess}");

                //DataTable table_mat_nos = JsonConvert.DeserializeObject<DataTable>(mat_nos);
                // 获取缓存
                var cachedUser = JsonConvert.DeserializeObject<User>(redisHelper.GetValue("user:1"));



                if (cachedUser != null)
                {
                    Console.WriteLine($"获取到缓存用户: {cachedUser.Name}");
                }

                this.TextBox1.Text = cachedUser.Name.ToString();




                //// 读取单条记录（同步）
                //var value1 = redisHelper.GetValue("key1");
                //Console.WriteLine($"Key1: {value1}");




                // 写入多条记录（异步）
                var multipleValues = new Dictionary<string, string>
                {
                    { "key3", "aasdfasdf" },
                    { "key4", "adfasdfasdf" }
                };
                redisHelper.SetValues(multipleValues, TimeSpan.FromMinutes(20));





                // 读取多条记录（异步）
                var keysToRead = new List<string> { "key3", "key4" };
                var results = redisHelper.GetValues(keysToRead);

                foreach (var kvp in results)
                {
                    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
                }



            }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        }


        protected void Button2_Click(object sender, EventArgs e)
        {



            using (var redisHelper = new StackExchange_RedisV2())
            {
                // 写入单条记录（同步）
                bool keySuccess = redisHelper.SetValue("key1v2", "value1", TimeSpan.FromSeconds(30));



                // 设置缓存
                var user = new User { Id = 1, Name = "John Doe soft sight", Email = "john@example.com" };



                var serializedValue = JsonConvert.SerializeObject(user);


                bool setSuccess = redisHelper.SetValue("user:1v2", serializedValue, TimeSpan.FromHours(1));

                // bool setSuccess = _cacheManager.Set("user:1", user, TimeSpan.FromHours(1));


                Console.WriteLine($"设置缓存是否成功: {setSuccess}");

                //DataTable table_mat_nos = JsonConvert.DeserializeObject<DataTable>(mat_nos);
                // 获取缓存
                var cachedUser = JsonConvert.DeserializeObject<User>(redisHelper.GetValue("user:1"));



                if (cachedUser != null)
                {
                    Console.WriteLine($"获取到缓存用户: {cachedUser.Name}");
                }

                this.TextBox1.Text = cachedUser.Name.ToString();




                //// 读取单条记录（同步）
                //var value1 = redisHelper.GetValue("key1");
                //Console.WriteLine($"Key1: {value1}");




                // 写入多条记录（异步）
                var multipleValues = new Dictionary<string, string>
                {
                    { "key3V", "aasdfasdf" },
                    { "key4V2", "adfasdfasdf" }
                };
                redisHelper.SetValues(multipleValues, TimeSpan.FromMinutes(20));





                // 读取多条记录（异步）
                var keysToRead = new List<string> { "key3V", "key4V2" };
                var results = redisHelper.GetValues(keysToRead);

                foreach (var kvp in results)
                {
                    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
                }



            }





        }


        public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
        }

        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public decimal Price { get; set; }
        }

        
    }
}